//
//  AllOrderFromViewController.m
//  JackDeve
//
//  Created by 王奎卿 on 2017/5/9.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "AllOrderFromViewController.h"
#import "OrderFromTableViewCell.h"
#import "Data.h"
#import "OrderFromModel.h"
#import "OrderModel.h"
#import "TTAccountTool.h"
#import "OrderFromViewController.h"


@interface AllOrderFromViewController ()<UITableViewDelegate,UITableViewDataSource,OrderFromDelegate>
{
    int sunNumber;
    float sumPrice;
    NSInteger indexRow;
    OrderFromTableViewCell *_cell;
    OrderFromViewController * v;
}
@property (nonatomic,strong) NSMutableArray *arrData;
@property (nonatomic,strong) NSMutableArray <OrderModel *>*orderData;
@end

@implementation AllOrderFromViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self getToken];
    
    [self showAllOrderView];
    
    self.allOrderV = self.view;
    
    [self.view addSubview:self.tableView];
}

- (void)getToken
{
    Data *data = [HomeUserDefault getUserDefault];
//    98caf517dbc497290b281010ec5365ea
    NSString *str = [NSString stringWithFormat:@"http://daohe.apic.1by1.cc/orders/index?token=%@&page_id=1",data.token];
    OrderFromModel *orderData = [[OrderFromModel alloc]init];
    orderData.delegate = self;
    [orderData getOrderData:str];
}

- (void)postOrderJson:(NSMutableArray *)array
{
    
    self.orderData = array;
    [self.tableView reloadData];
    
}

- (void)showAllOrderView
{
    // 对tableView进行包装
    CGSize size = [UIScreen mainScreen].bounds.size;
    self.tableView = ({
    
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 110, size.width, size.height-110) style:UITableViewStyleGrouped];
        
        tableView.delegate = self;
        
        tableView.dataSource = self;
        
        tableView.tableFooterView = [[UIView alloc] init];
        
        tableView;
    });
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _orderData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (void)setIsAni:(BOOL)isAni {
    _isAni = isAni;
    [self.tableView setEditing:!self.tableView.editing animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    indexRow = indexPath.section;
    
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", indexPath.section, indexPath.row];
    //出列可重用的cell ，通俗理解:从缓存池取标识为"Cell"的cell
    _cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (_cell == nil) {
        _cell = [[OrderFromTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // 取消选中状态
    [_cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    OrderModel *model = _orderData[indexPath.section];
    //订单状态：20已支付或到付，25商家处理，30完成
    if ([model.order_status isEqualToString:@"20"]) {
        _cell.order_status.text = @"已支付";
        
        [_cell.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cell.cancelBtn addTarget:self action:@selector(quxiao) forControlEvents:UIControlEventTouchUpInside];
    }else if ([model.order_status isEqualToString:@"25"]){
        _cell.order_status.text = @"正在派送";
        _cell.order_status.textColor = [UIColor colorWithRed:0.467 green:1.000 blue:0.452 alpha:1.000];
        [_cell.cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [_cell.cancelBtn addTarget:self action:@selector(quxiao) forControlEvents:UIControlEventTouchUpInside];
    }else if ([model.order_status isEqualToString:@"30"]){
        _cell.order_status.text = @"订单完成";
        _cell.cancelBtn.hidden = YES;
    }
    //判断商品个数，进行赋值
    if (self.orderData[indexRow].goods.count == 0) {

        _cell.goods_one.hidden = YES;
        _cell.count_one.hidden = YES;
        _cell.goods_two.hidden = YES;
        _cell.count_two.hidden = YES;
        _cell.goods_three.hidden = YES;
        _cell.count_three.hidden = YES;
        _cell.ellipsis.hidden = YES;
        _cell.total.hidden = YES;
        _cell.total_price.hidden = YES;



        
    }if (self.orderData[indexRow].goods.count == 1) {
        _cell.goods_one.text = [_orderData[indexPath.section].goods[0] objectForKey:@"goods_name"];
        _cell.count_one.text = [NSString stringWithFormat:@"X%@", [_orderData[indexPath.section].goods[0] objectForKey:@"num"]];

        _cell.goods_two.hidden = YES;
        _cell.count_two.hidden = YES;
        _cell.goods_three.hidden = YES;
        _cell.count_three.hidden = YES;
        _cell.ellipsis.hidden = YES;
  
        
    }if (self.orderData[indexRow].goods.count == 2) {
        _cell.goods_one.text = [_orderData[indexPath.section].goods[0] objectForKey:@"goods_name"];
        _cell.count_one.text = [NSString stringWithFormat:@"X%@", [_orderData[indexPath.section].goods[0] objectForKey:@"num"]];
        _cell.goods_two.text = [_orderData[indexPath.section].goods[1] objectForKey:@"goods_name"];
        _cell.count_two.text = [NSString stringWithFormat:@"X%@",[_orderData[indexPath.section].goods[1] objectForKey:@"num"]];
        
        _cell.goods_three.hidden = YES;
        
        _cell.count_three.hidden = YES;
        _cell.ellipsis.hidden = YES;
        
    }if (self.orderData[indexRow].goods.count == 3) {
        _cell.goods_one.text = [_orderData[indexPath.section].goods[0] objectForKey:@"goods_name"];
        _cell.count_one.text = [NSString stringWithFormat:@"X%@", [_orderData[indexPath.section].goods[0] objectForKey:@"num"]];
        _cell.goods_two.text = [_orderData[indexPath.section].goods[1] objectForKey:@"goods_name"];
        _cell.count_two.text = [NSString stringWithFormat:@"X%@", [_orderData[indexPath.section].goods[1] objectForKey:@"num"]];
        _cell.goods_three.text = [_orderData[indexPath.section].goods[2] objectForKey:@"goods_name"];
        _cell.count_three.text = [NSString stringWithFormat:@"X%@",[_orderData[indexPath.section].goods[2] objectForKey:@"num"]];
        
        _cell.ellipsis.hidden = YES;
        
        
    }if (self.orderData[indexRow].goods.count >3) {
        _cell.goods_one.text = [_orderData[indexPath.section].goods[0] objectForKey:@"goods_name"];
        _cell.count_one.text = [NSString stringWithFormat:@"X%@", [_orderData[indexPath.section].goods[0] objectForKey:@"num"]];
        _cell.goods_two.text = [_orderData[indexPath.section].goods[1] objectForKey:@"goods_name"];
        _cell.count_two.text = [NSString stringWithFormat:@"X%@", [_orderData[indexPath.section].goods[1] objectForKey:@"num"]];
        _cell.goods_three.text = [_orderData[indexPath.section].goods[2] objectForKey:@"goods_name"];
        _cell.count_three.text = [NSString stringWithFormat:@"X%@",[_orderData[indexPath.section].goods[2] objectForKey:@"num"]];
        _cell.ellipsis.text = @"...";
    }
    
    for (int i = 0; i < self.orderData[indexRow].goods.count; i++) {
        int num = [[self.orderData[indexRow].goods[i] objectForKey:@"num"] intValue];
        
        float price = [[self.orderData[indexRow].goods[i] objectForKey:@"goods_price"] floatValue];
        //、、个数
        sunNumber += num;
        //总价so
        sumPrice += num*price;
    }    Data *data = [HomeUserDefault getUserDefault];
    [_cell.userImg sd_setImageWithURL:[NSURL URLWithString:data.avatar]];
    
    _cell.diner_name.text = model.diner_name;
    _cell.total.text = [NSString stringWithFormat:@"共%d件商品,实付",sunNumber];
    _cell.total_price.text = [NSString stringWithFormat:@"¥%.1f",sumPrice];
    

    sumPrice = 0.0;
    sunNumber = 0;
    
    
    [_cell.againBtn setTitle:@"再来一单" forState:UIControlStateNormal];
    [_cell.againBtn addTarget:self action:@selector(again) forControlEvents:UIControlEventTouchUpInside];
    return _cell;

}


// 提交编辑样式
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingStyle = UITableViewCellEditingStyleDelete;
    
    if (indexRow >= _orderData.count) {
        return;
    }
    
    OrderModel *model = _orderData[indexRow];
    Data *data = [HomeUserDefault getUserDefault];
    
    NSString *tr = [NSString stringWithFormat:@"http://daohe.apic.1by1.cc/orders/delete?token=%@&order_sn=%@",data.token,model.order_sn];
    TTAccountTool *account = [[TTAccountTool alloc] init];
    [account deleteDingDan:tr];
  //开始或者结束编辑状态
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    if (self.butBlock != nil) {
        self.butBlock(@"编辑");
    }
    [self.tableView reloadData];
    [self.tableView setEditing:!self.tableView.editing animated:YES];
}

#pragma mark 再来一单
-(void)again{
   
    OrderModel *sub = _orderData[indexRow];
    Data *login = [HomeUserDefault getUserDefault];
    NSString *tr = [NSString stringWithFormat:@"http://daohe.apic.1by1.cc/orders/buy_again?token=%@&order_sn=%@",login.token,sub.order_sn];
    
    
    
    
    TTAccountTool *account = [[TTAccountTool alloc] init];
    [account againDingDan:tr];
}


#pragma mark 取消订单
-(void)quxiao{
  
    OrderModel *sub = _orderData[indexRow];
    //如果等于20，订单可以取消
    if ([sub.order_status isEqualToString:@"20"]) {
        Data *login = [HomeUserDefault getUserDefault];
        NSString *tr = [NSString stringWithFormat:@"http://daohe.apic.1by1.cc/orders/cancel?token=%@&order_sn=%@",login.token,sub.order_sn];
        TTAccountTool *account = [[TTAccountTool alloc] init];
        [account cancelDingDan:tr];
    }else{
        [SVProgressHUD showErrorWithStatus:@"不能取消订单"];
        [SVProgressHUD dismissWithDelay:1.5];
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CGFloat height;
    
    if (self.orderData[indexRow].goods.count == 0) {
        height = 0;
        return height;
    }if (self.orderData[indexRow].goods.count == 1) {
        height = 160;
        return height;
    }if (self.orderData[indexRow].goods.count == 2) {
        height = 190;
        return height;
    }if (self.orderData[indexRow].goods.count == 3) {
        height = 220;
        return height;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

@end

AllOrderFromViewController *allOrder(id name)
{
    AllOrderFromViewController *all = [[AllOrderFromViewController alloc]init];
    
    return all;
}
